<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <j-form-container :disabled="formDisabled">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="菜单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-radio-group v-model="radioValue" @change="change" :default-value="1">
                  <a-radio :value="1">一级菜单</a-radio>
                  <a-radio :value="2">子菜单</a-radio>
                </a-radio-group>
              </a-form-model-item>
            </a-col>
            <a-col :span="24" v-if="radioType == 2" >
              <a-form-model-item label="上级菜单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentId">
                <a-tree-select
                  v-model="model.parentId"
                  style="width: 100%"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="treeData"
                  :replaceFields="dataFields"
                  placeholder="请选择上级菜单"
                  tree-default-expand-all
                >
                </a-tree-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="产品类型名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productType">
                <a-input v-model="model.productType" placeholder="请输入产品类型名称"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
                <a-input v-model="model.remarks" placeholder="请输入描述"  ></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
  </j-modal>
</template>

<script>
  import { getAction, httpAction } from '@/api/manage'
  export default {
    name: 'productTypeModal',
    data () {
      return {
        title:'',
        width: 800,
        radioType: '',
        visible: false,
        disableSubmit: false,
        radioValue: 1,
        treeData: [],
        dataFields: {
          title: 'productType',
          key: 'id',
          value: 'id',
          children: "children"
        },
        value: '',
        model:{},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
         validatorRules: {
          parentId: [{ required: true, message: '请选择上级菜单' }],
          productType: [{ required: true, message: '请输入产品名称' }],
        },
        url: {
          add: "/ghProductType/add",
          edit: "/ghProductType/edit",
          queryById: "/ghProductType/queryById",
          getParent: "/ghProductType/getProductParent"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      }
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        if(!record.parentId) {
          this.radioValue = 1
          this.radioType = 1
        } else {
          this.radioValue = 2
          this.radioType = 2
          this.initData();
        }
      },
      close () {
        this.visible = false;
        this.radioType = 1;
        this.model = {};
        this.radioValue = 1;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }

            if(this.radioValue == 1) {
              this.$set(this.model, 'parentId', '')
            }

            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.radioValue = 1;
              that.close();
            })
          }
        })
      },
      submitCallback(){
        this.visible = false;
      },
      handleCancel () {
        this.close()
      },
      change(e) {
        this.radioType = e.target.value
        this.initData();
      },
      handleParentIdChange(value) {
        
      },
      initData() {
        getAction(this.url.getParent).then(res => {
          if (res.success) {
            this.treeData = res.result;
          }
        });
      },
    }
  }
</script>